﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureFamilyTemplateDocumentUtils.h"
#include "FilePath.h"
#include "IFamilyManager.h"
#include "IDocumentManager.h"
#include "IUserTransaction.h"
#include "FamilyDocumentUtility.h"
#include "FamStructureTemplateDocument.h"
#include "StructureDefaultFamilyTypes.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace gfam;

bool gcmp::StructureFamilyTemplateDocumentUtils::CreateBeamTemplateFamilyDocument(const std::wstring& filePathName)
{
    UniIdentity category = BuiltInCategoryUniIdentities::BICU_STRUCTURE_BEAM;
    std::wstring familyName = FilePath(filePathName).GetFileNameWithoutExtension();
    IDocument* pDoc = FamilyDocumentUtility::CreateFamilyDocument(category, familyName);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IFamilyManager* pFamilyManager = IFamilyManager::Get(pDoc);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFamilyManager, L"pFamilyManager为空",L"GDMPLab",L"2024-03-30");

    {
        OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"创建梁族模板"), true);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opTransaction, L"opTransaction为空",L"GDMPLab",L"2024-03-30");

        // 初始化族模板
        FamStructureTemplateDocument::InitBeamTemplateFamilyDocument(pDoc);

        // 设置当前族类型
        pFamilyManager->RenameCurrentFamilyType(DEFAULT_TEMPLATE);

        opTransaction->CommitAndResetHistory();
    }

    const std::wstring familyPath = filePathName;
    DocumentSaveOptions saveOptions(false);
    DocumentSaveResult saveResult;
    pDoc->SaveAs(familyPath, saveOptions, saveResult);
    DBG_WARN_AND_RETURN_UNLESS(saveResult.Code== DocumentSaveError::Ok, L"", L"族文件保存失败",L"GDMPLab",L"2024-03-30");
    RuntimeId runtimeID = pDoc->GetRuntimeId();
    IDocumentManager::Get()->CloseDocument(pDoc->GetRuntimeId());

    return true;
}

bool gcmp::StructureFamilyTemplateDocumentUtils::CreateColumnTemplateFamilyDocument(const std::wstring& filePathName)
{
    UniIdentity category = BuiltInCategoryUniIdentities::BICU_STRUCTURE_COLUMN;
    std::wstring familyName = FilePath(filePathName).GetFileNameWithoutExtension();
    IDocument* pDoc = FamilyDocumentUtility::CreateFamilyDocument(category, familyName);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IFamilyManager* pFamilyManager = IFamilyManager::Get(pDoc);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFamilyManager, L"pFamilyManager为空",L"GDMPLab",L"2024-03-30");

    {
        OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"创建柱族模板"), true);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(opTransaction, L"opTransaction为空",L"GDMPLab",L"2024-03-30");

        // 初始化族模板
        FamStructureTemplateDocument::InitColumnTemplateFamilyDocument(pDoc);

        // 设置当前族类型
        pFamilyManager->RenameCurrentFamilyType(DEFAULT_TEMPLATE);
        //添加参照平面
        FamStructureTemplateDocument::AddColumnFamilyTemplateReferencePlanes(pDoc);
        opTransaction->CommitAndResetHistory();
    }

    const std::wstring familyPath = filePathName;
    DocumentSaveOptions saveOptions(false);
    DocumentSaveResult saveResult;
    pDoc->SaveAs(familyPath, saveOptions, saveResult);
    DBG_WARN_AND_RETURN_UNLESS(saveResult.Code == DocumentSaveError::Ok, L"", L"族文件保存失败",L"GDMPLab",L"2024-03-30");
    RuntimeId runtimeID = pDoc->GetRuntimeId();
    IDocumentManager::Get()->CloseDocument(pDoc->GetRuntimeId());
    return true;
}
